import * as NSBase from '../../Common/NetServiceBase';
import {
	T_MINI_PROGRAM,
	TYConfig,
	Wx_Page,
	Wx_PageData,
	WXDataClassRuntimeDecorator,
	Logger,
	LogLevel
	} from '../EntityBase';

//NS.QueryMiniProgram
@WXDataClassRuntimeDecorator()
export class RecommendMiniProgramBasePageData extends Wx_PageData {
	public MiniPrograms: Array<T_MINI_PROGRAM> = new Array<T_MINI_PROGRAM>();
}

export class RecommendMiniProgramBasePage extends Wx_Page {
	public Data = new RecommendMiniProgramBasePageData();

	/**
     * 初始化页面信息
     * 
     * @memberof RecommendPage
     */
	public async InitData(options: any) {
		try {
			wx.showLoading({ title: "加载中···" });
			this.Data.MiniPrograms=await NSBase.QueryMiniProgram(TYConfig.QueryMiniProgram);
			await this.Data.ApplyChange(this);
		} catch (e) {
			this.ShowError(e)
		} finally {
			wx.hideLoading();
		}
	}

	public NavigateToMiniProgramHandler(e: any): void {
		let el = this.Data.MiniPrograms[e.currentTarget.dataset.index];
		if (el.ISSHOWIMG) {
			if (!el.IMG.IsNullOrWhiteSpace())
				wx.previewImage({ urls: [el.IMG + '?t=' + this.App.SYS_GetNowDate().getTime()] });
		} else {
			if (this.Data.CanNavigateToMiniProgramForAPI && !el.ISSHOWIMG) {
				wx.navigateToMiniProgram({
					appId: el.APPID,
					path: el.PATH,
					extraData: el.EXTRADATA ? JSON.parse(el.EXTRADATA) : {},
					envVersion: el.ENVVERSION,
					success(res) {
						// 打开成功
						Logger.Debug(LogLevel.L1, res);
					}
				});
			}
		}
	}

	public RefreshHandler(e:any):void{
		this.InitData(null);
	}
}